/**
 * 登入模块
 */
var netcore = require('../../../utils/network.js')
var wxCrypt = require('../../../utils/WXBizDataCrypt.js')
var tripleDes = require('../../../utils/CKCrypto.js')
/**
 * 登入
 */
function requestLogin(phone, code, shareAk,callBack){
  var params = new Map()
  params.set('userPhone', tripleDes.enCode(phone))
  params.set('smsCode', tripleDes.enCode(code))
  if (shareAk!=null&&typeof(shareAk) !=undefined &&shareAk.length>0){
    params.set("shareAk",shareAk)
  }
  params.set("latitude", getApp().globalData.userLocationInfo.lat)
  params.set("longitude", getApp().globalData.userLocationInfo.lng)
  params.set("address", getApp().globalData.userLocationInfo.address)
  netcore.requestPost("account/accountLogin.do", params, function (success) {
    wx.setStorage({
      key: 'userToken',
      data: success.userToken,
    })
    wx.setStorage({
      key: 'phone',
      data: success.phone,
    })
    callBack(success)
  }, function (errorCode,errMsg) {
    console.log(errMsg)
    wx.showToast({
      title: errMsg,
      icon: 'none',
      duration:2000
    })
  })
}
function requestwx(origin,code,callBack) {
  var params = new Map()
  params.set('origin', origin)
  params.set('wxCode', code)
  params.set("latitude", getApp().globalData.userLocationInfo.lat)
  params.set("longitude", getApp().globalData.userLocationInfo.lng)
  params.set("address", getApp().globalData.userLocationInfo.address)
  netcore.requestPost("account/authLogin.do", params, function (success) {
    wx.setStorage({
      key: 'userToken',
      data: success.userToken,
    })
    wx.setStorage({
      key: 'phone',
      data: success.phone,
    })
    callBack(success)
  }, function (errorCode, errMsg) {
    console.log(errMsg)
    wx.showToast({
      title: errMsg,
      icon: 'none',
      duration: 2000
    })
  })
}

function requestwxbd(origin, code, callBack) {
  var params = new Map()
  params.set('origin', origin)
  params.set('code', code)
  console.log('queryBills.params=>', params)
  netcore.requestPost("account/bindWx.do", params, function (success) {
    wx.showToast({
      title: '绑定成功，可以使用微信登入啦',
      icon:'none'
    })
    // getOpenId(function (openId) {
    //   if (openId != null && typeof (openId) != undefined) {
    //     success['openId'] = openId
    //   } else {
    //     success['openId'] = ''
    //   }
    //   callBack(success)
    // })
  }, function (errorCode, errMsg) {
    console.log(errMsg)
    wx.showToast({
      title: errMsg,
      icon: 'none',
      duration: 2000
    })
  })
}



function getCode(phone,callBack){
  var params = new Map()
  params.set('phone', tripleDes.enCode(phone))
  params.set('action', "loginOrRegister")
  netcore.requestPost("phone/sendSmsCode.do",params,function(data){
    callBack(data)
  }, function (errorCode, errMsg){
    wx.showToast({
      title: errMsg,
      icon: 'none',
    })
  })
}
function getOpenId(callBack){
  wx.login({
    success: res => {
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      if (res.code) {
        console.log("5555");
        console.log(res)
        var map = new Map()
        map.set("code", res.code)
        params.set('origin', 'xcx')
        netcore.requestPost("appshare/getMinOpenId.do", map, function (success) {
          callBack(success.openid)
        }, function (errorCode, errMsg) {
          console.log(errMsg)
          callBack(null)
        })
      }
    }
  })
}

module.exports = {
  getOpenId: getOpenId,
  requestLogin: requestLogin,
  requestwxbd: requestwxbd,
  requestwx: requestwx,
  getCode: getCode

}